<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="500px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$t('roleCode')" prop="code" :error="error.code">
        <el-input
          v-model="form.code"
          maxlength="32"
          :disabled="!createFlag"
          show-word-limit
          :placeholder="$t('inputRoleCode')"
        />
      </el-form-item>
      <el-form-item
        :label="$t('roleZHName')"
        prop="name_zh"
        :error="error.name_zh"
      >
        <el-input
          v-model="form.name_zh"
          maxlength="128"
          show-word-limit
          :placeholder="$t('inputRoleZHName')"
        />
      </el-form-item>
      <el-form-item
        :label="$t('roleENName')"
        prop="name_en"
        :error="error.name_en"
      >
        <el-input
          v-model="form.name_en"
          maxlength="128"
          show-word-limit
          :placeholder="$t('inputRoleENName')"
        />
      </el-form-item>
      <el-form-item
        :label="$t('roleDesc')"
        prop="content"
        :error="error.content"
      >
        <el-input
          v-model="form.content"
          type="textarea"
          maxlength="200"
          show-word-limit
          :autosize="{ minRows: 2, maxRows: 4 }"
          :placeholder="$t('inputRoleDesc')"
        />
      </el-form-item>
      <el-form-item :label="$t('administrator')" prop="is_admin">
        <el-switch v-model="form.is_admin" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" @click="submitForm">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" @click="submitForm">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  updateRoleListApi,
  createRoleListApi
} from '@/views/rbac/api/rbac.api';
export default {
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      roleId: undefined,
      form: {
        code: undefined,
        name_zh: undefined,
        name_en: undefined,
        content: undefined,
        is_admin: false
      },
      error: {
        code: undefined,
        name_zh: undefined,
        name_en: undefined,
        content: undefined
      }
    };
  },
  computed: {
    rules() {
      return {
        code: [
          {
            required: true,
            message: this.$t('roleCodeRequire'),
            trigger: 'blur'
          }
        ],
        name_zh: [
          {
            required: true,
            message: this.$t('roleZHNameRequire'),
            trigger: 'blur'
          }
        ],
        name_en: [
          {
            required: true,
            message: this.$t('roleENNameRequire'),
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: this.$t('roleContentRequire'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async openSheetDialog(data) {
      this.openSheet = !this.openSheet;
      this.reset();
      if (data) {
        this.title = this.$t('roleUpdate');
        this.createFlag = false;
        this.roleId = data.id;
        this.form = data;
      } else {
        this.title = this.$t('roleCreate');
        this.createFlag = true;
        this.roleId = undefined;
      }
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form)
    },
    submitForm: function() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.resetJsonObject(this.error)
          var resp;
          if (this.createFlag) {
            resp = await createRoleListApi(this.form);
          } else {
            resp = await updateRoleListApi(this.roleId, this.form);
          }
          if (resp.success) {
            this.$emit('refreshList');
            this.cancel();
            if (this.createFlag) {
              this.targaMessage(
                this.$t('createSuccess', {
                  title: this.$t('roleCode'),
                  value: this.form.code
                })
              );
            } else {
              this.targaMessage(
                this.$t('updateSuccess', {
                  title: this.$t('roleCode'),
                  value: this.form.code
                })
              );
            }
          } else {
            if (typeof resp.msg === 'object') {
              this.error = resp.msg
            } else {
              this.targaMessageBox(this, resp.msg)
            }
          }
        }
      });
    }
  }
};
</script>

<style></style>
